package com.mingqi.enetconnected.view.Dialog.single;

import android.app.Activity;
import android.app.Dialog;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.util.resource.ResourceUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by li on 2018/8/31.
 * WeChat 18571658038
 * author LiWei
 */

public class SinglePickerManager {

    private Activity mActivity;
    private Dialog mDialog;
    private TextView mTvCancel;
    private TextView mTvConfirm;
    private TextView mTvTitle;
    private ListView mLvSingle;
    private onConfirmClickListener mConfirmListener;

    public SinglePickerManager(Activity activity, int style) {
        mActivity = activity;
        initDialog(style);
    }

    public SinglePickerManager(Activity activity) {
        mActivity = activity;
        initDialog(R.style.my_bottom_dialog);
    }

    private void initDialog(int style) {
        if (style == -1) {
            mDialog = new Dialog(mActivity);
        } else {
            mDialog = new Dialog(mActivity, style);
        }
        mDialog.setContentView(mActivity.getLayoutInflater().inflate(R.layout.dialog_single_picker, null));
        Display dd = mActivity.getWindowManager().getDefaultDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        dd.getMetrics(dm);
        WindowManager.LayoutParams attributes = mDialog.getWindow().getAttributes();
        if (style == -1) {
            mDialog.getWindow().setGravity(Gravity.CENTER);
        } else {
            mDialog.getWindow().setGravity(Gravity.BOTTOM);
        }

        attributes.height = (int) (dm.heightPixels * 0.5);
        attributes.width = dm.widthPixels;

        mTvCancel = mDialog.findViewById(R.id.tvCancel);
        mTvConfirm = mDialog.findViewById(R.id.tvConfirm);
        mTvTitle = mDialog.findViewById(R.id.tvTitle);
        mLvSingle = mDialog.findViewById(R.id.lv_single);

        initData();
        setListener();

        mTvTitle.setText(ResourceUtils.getString(R.string.please_choose_single));
    }

    private void initData() {

        List<String> stringList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {

            stringList.add("选项：" + i);
        }
        SinglePickerAdapter adapter = new SinglePickerAdapter(stringList);
        mLvSingle.setAdapter(adapter);
    }

    private void setListener() {

        mTvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mConfirmListener != null) {

                    int checkedItemPosition = mLvSingle.getCheckedItemPosition();
                    mConfirmListener.onClick(checkedItemPosition);
                }
            }
        });

        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();
            }
        });
    }

    public void setOnConfirmListener(onConfirmClickListener listener) {

        mConfirmListener = listener;
    }

    public interface OnCancelClickListener {

        void onClick();
    }

    public interface onConfirmClickListener {

        void onClick(int checkedItemPosition);
    }

    public void dismiss() {

        if (mDialog != null) {
            mDialog.cancel();
        }
    }

    public void show() {

        if (mDialog != null) {
            mDialog.show();
        }
    }
}
